<?php
/**
 * Created by PhpStorm.
 * User: Lin
 * Date: 2017/3/20 0020
 * Time: 下午 21:12
 */
class OrderAction extends CommonAction{

    //显示未确定订单视图
    public function showList(){
        $map=array(
            '_logic'=>'or',
            'pay_status'=>PAY_STATUS_1,
            'payment_id'=>3
        );
        $where=array(
            'order_status'=>ORDER_STATUS_0,
            '_complex'=>$map,
        );
        import('Class.Order',APP_PATH);
        import('Class.Payment',APP_PATH);
        $orderArr=Order::getOrderArr($where);
        foreach($orderArr as $k=>$v){
            $orderArr[$k]['payment']=Payment::getPaymentName($v['payment_id']);
            $orderArr[$k]['pay_status']=Payment::getPaymentStatusName($v['pay_status']);
            $orderArr[$k]['order_status']=Order::getOrderStatusName($v['order_status']);
        }
        $this->orderArr=$orderArr;
//        p($orderArr,1);
        $this->display();
    }

    //显示订单详细信息
    public function orderInfo(){
        $orderId=$_GET['orderId'];
        import('Class.Order',APP_PATH);
        import('Class.Payment',APP_PATH);
        $order=new Order();
        $payment=new Payment();
        if($_GET['is_RX']){
            $orderInfo=D('RxRelation')->relation(true)->where('id='.$orderId)->find();
            $orderInfo['order_goods'][0]=array(
                'goods_id' => $orderInfo['goods_id'],
                'num' => $orderInfo['num'],
                'shop_price' => $orderInfo['shop_price'],
                'goods'=>$orderInfo['goods'],
            );
            $orderInfo['order_sn']=$orderInfo['rx_sn'];
            $orderInfo['payment']['name']=M('payment')->where('id=3')->getField('name');
            $orderInfo['consignee_name']=$orderInfo['rx_name'];
            $orderInfo['consignee_address']=$orderInfo['store']['address'];
            $orderInfo['telephone']=$orderInfo['rx_phone'];
            $orderInfo['total']=$orderInfo['sum'];
            $orderInfo['shipping']['name']=M('shipping')->where('id=4')->getField('name');
            $orderInfo['pay_status']=RX_STATUS_0_NAME;
            $orderInfo['order_status']=Order::getOrderStatusName($orderInfo['order_status']);
//        $orderInfo['payment']=$payment->getPaymentName($orderInfo['payment_id']);
            $orderInfo['pay_status']=$payment->getPaymentStatusName($orderInfo['pay_status']);
        }else{
            $orderInfo=$order->getOrderInfo($orderId);
            foreach ($orderInfo['order_goods'] as $k=>$v) {
                $orderInfo['order_goods'][$k]['goods']=M('goods')->where(array('id' => $v['goods_id']))->find();
            }
//        $orderInfo['payment']=$payment->getPaymentName($orderInfo['payment_id']);
            $orderInfo['pay_status']=$payment->getPaymentStatusName($orderInfo['pay_status']);
        }

        $orderInfo['order_status']=$order->getOrderStatusName($orderInfo['order_status']);
//        p($orderInfo,1);
        $this->orderInfo=$orderInfo;
        $this->display();
    }

    //显示完成订单列表视图
    public function showFinishList(){
        $where=array(
            'order_status'=>array('in',array(ORDER_STATUS_2,ORDER_STATUS_3))
        );
        import('Class.Order',APP_PATH);
        import('Class.Payment',APP_PATH);
        $orderArr=Order::getOrderArr($where);
        foreach($orderArr as $k=>$v){
            $orderArr[$k]['payment']=Payment::getPaymentName($v['payment_id']);
            $orderArr[$k]['pay_status']=Payment::getPaymentStatusName($v['pay_status']);
            $orderArr[$k]['order_status']=Order::getOrderStatusName($v['order_status']);
        }
        $this->orderArr=$orderArr;
        $this->display();
    }

    //显示待发货订单列表视图
    public function shipping(){
        //订单发货表
        $where=array(
            'order_status'=>ORDER_STATUS_1
        );
        import('Class.Order',APP_PATH);
        import('Class.Payment',APP_PATH);
        $orderArr=Order::getOrderArr($where);
        foreach($orderArr as $k=>$v){
            $orderArr[$k]['payment']=Payment::getPaymentName($v['payment_id']);
            $orderArr[$k]['pay_status']=Payment::getPaymentStatusName($v['pay_status']);
            $orderArr[$k]['order_status']=Order::getOrderStatusName($v['order_status']);
        }
        //需求登记发货表
        $where1=array(
            'rx_status'=>RX_STATUS_1,
            'order_status'=>ORDER_STATUS_1
        );
        $rxArr=D('RxRelation')->relation(true)->where($where1)->select();
        foreach($rxArr as $k=>$v){
            $rxArr[$k]['payment']=RX_STATUS_1_NAME;
            $rxArr[$k]['order_sn']=$v['rx_sn'];
            $rxArr[$k]['pay_status']=Payment::getPaymentStatusName(PAY_STATUS_0);
            $rxArr[$k]['order_status']=Order::getOrderStatusName($v['order_status']);
            $rxArr[$k]['consignee_name']=$rxArr[$k]['rx_name'];
            $rxArr[$k]['consignee_address']=$rxArr[$k]['store']['address'];
            $rxArr[$k]['telephone']=$rxArr[$k]['rx_phone'];
            $rxArr[$k]['total']=$rxArr[$k]['sum'];
        }

        $this->orderArr=is_array($orderArr)?(is_array($rxArr)?array_merge($orderArr,$rxArr):$orderArr):$rxArr;
        $this->display();
    }

    //显示需求登记列表订单
    public function showListRX(){
        import('Class.Order',APP_PATH);
        import('Class.Payment',APP_PATH);
        
        $rxArr=D('RxRelation')->relation('store')->where('rx_status='.RX_STATUS_0)->select();
        $shipping=M('shipping')->where('id=4')->getField('name');
        foreach($rxArr as $k=>$v){
            $rxArr[$k]['order_sn']=$v['rx_sn'];
            $rxArr[$k]['consignee_name']=$v['rx_name'];
            $rxArr[$k]['consignee_address']=$v['store']['address'];
            $rxArr[$k]['telephone']=$v['rx_phone'];
            $rxArr[$k]['payment']=$shipping;
            $rxArr[$k]['total']=$v['sum'];
            $rxArr[$k]['pay_status']=RX_STATUS_0_NAME;
            $rxArr[$k]['order_status']=Order::getOrderStatusName($v['order_status']);
        }
//        p($rxArr,1);
        $this->rxArr=$rxArr;
        $this->display();
    }

    //显示完成登记列表视图
    public function showFinishListRX(){
        import('Class.Order',APP_PATH);
        import('Class.Payment',APP_PATH);
        $rxArr=D('RxRelation')->relation('store')->where('rx_status!='.RX_STATUS_0)->select();
        $shipping=M('shipping')->where('id=4')->getField('name');
        foreach($rxArr as $k=>$v){
            $rxArr[$k]['order_sn']=$v['rx_sn'];
            $rxArr[$k]['consignee_name']=$v['rx_name'];
            $rxArr[$k]['consignee_address']=$v['store']['address'];
            $rxArr[$k]['telephone']=$v['rx_phone'];
            $rxArr[$k]['payment']=$shipping;
            $rxArr[$k]['total']=$v['sum'];
            $rxArr[$k]['pay_status']=constant('RX_STATUS_'.$rxArr[$k]['rx_status'].'_NAME');
            $rxArr[$k]['order_status']=Order::getOrderStatusName($v['order_status']);
        }
//        p($rxArr,1);
        $this->rxArr=$rxArr;
        $this->display();
    }

    //ajax确认订单
    public function sureOrder(){
        $id=$_POST['id'];
        import('Class.Order',APP_PATH);
        $result=Order::changeOrderStatus(ORDER_STATUS_1,$id);
        exit($result);
    }
    
    //ajax完成订单
    public function success(){
        $id=$_POST['id'];
        $isRX=$_POST['is_RX'];
        import('Class.Order',APP_PATH);
        $data=array(
            'order_status'=>ORDER_STATUS_2,
            'pay_status'=>PAY_STATUS_1
        );
        $result=Order::saveOrder($data,$id,$isRX);
        exit($result);
    }

    //ajax取消订单
    public function closeOrder(){
        $id=$_POST['id'];
        $isRX=$_POST['is_RX'];
        import('Class.Order',APP_PATH);
        $data=array(
            'order_status'=>ORDER_STATUS_3,
            'pay_status'=>PAY_STATUS_0
        );
        $result=Order::saveOrder($data,$id,$isRX);
        exit($result);
    }

    //ajax处方登记审核
    public function checkRx(){
        $id=$_POST['id'];
        $pass=$_POST['pass'];
        import('Class.Order',APP_PATH);
        $result=Order::editRxStatus($id,$pass);
        exit($result);
    }
}